
package com.common.android.utils.task.progressTask;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.AsyncTask;
import android.view.KeyEvent;
import android.widget.Toast;

/**
 * @ClassName: ProgressTask
 * @Description: 实现后台处理，前台遮当操作类
 * @author Administrator
 * @date 2012-2-7 下午08:25:40
 */
public abstract class ProgressTask extends AsyncTask<Void, Integer, Integer> {
	public interface IProgressDialogIteration{
		public void onShow();
		public void onDismiss();
	}
	protected IProgressDialogIteration progressDialogIteration;
    protected ProgressDialog mPd;

    protected StringBuilder mErrorMsg;

    protected Context mContext;

    /**
     * @param context{当前活动}
     * @param resId{提示信息}
     */
    public ProgressTask(Context context, int resId) {
        super();
        mContext = context;
    	mPd = new ProgressDialog(mContext);
        mPd.setCancelable(false);
        mPd.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                // 屏蔽Search键(因为按搜索按键会被关闭)
                switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_SEARCH:
                    return true;
                case KeyEvent.KEYCODE_BACK:
                    cancelTask();
                    // cancelConfirm();
                    return true;
                default:
                    return false;
                }
            }
        });
        mPd.setMessage(mContext.getResources().getString(resId));
        mErrorMsg = new StringBuilder();
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (progressDialogIteration!=null){
        	progressDialogIteration.onShow();
        }else{
        	mPd.show();	
        }
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        //showToast(mContext, R.string.nd_user_cancel);
    }

    @Override
    protected void onPostExecute(Integer result) {
        try {
            super.onPostExecute(result);
            if (!this.isCancelled()) {
                dismiss();
                if (result == 0) {
                    // 成功
                    doSuccess();
                } else {
                    // 失败
                    doFail(result);
                }
            }
        } catch (Exception e) {
        	e.printStackTrace();
        }
    }

    // 执行成功处理
    protected abstract void doSuccess();

    /**
     * @Title: doFail
     * @Description: (执行失败处理 默认是把错误信息show处理)
     * @author Administrator
     * @date 2012-2-7 下午08:27:23
     * @param result
     * @return void
     * @throws
     */
    protected void doFail(int result) {
        if (mErrorMsg.length() <= 0) {
            showToast(mContext, result);
        } else {
            showToast(mContext, mErrorMsg.toString());
        }
    }

    private void cancelTask() {
    	cancel(true);//bug 要设置标志位 界面判断才会对，才能关闭对话框
    	dismiss();
        
    }
    
    private void dismiss(){
    	if (progressDialogIteration!=null){
    		progressDialogIteration.onDismiss();
    	}else{
    		if ((mPd != null) && (mPd.isShowing())) {
                mPd.dismiss();
            }	
    	}
    }

    /**
     * 终止操作确认
     */
    // private void cancelConfirm() {
    // AlertDialog.Builder dlg = new AlertDialog.Builder(mContext);
    // dlg.setTitle(R.string.nd_confirm);
    // dlg.setMessage(R.string.nd_cancel_confirm);
    // dlg.setPositiveButton(R.string.nd_ok, new
    // DialogInterface.OnClickListener() {
    // @Override
    // public void onClick(DialogInterface dialog, int which) {
    // cancelTask();
    // }
    // });
    // dlg.setNegativeButton(R.string.nd_cancel, null);
    // dlg.show();
    // }

    public void showToast(Context context, int strId) {
        try {
            Toast.makeText(context, context.getResources().getString(strId), Toast.LENGTH_SHORT)
                    .show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showToast(Context context, String str) {
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }

}
